package com.wlyuan.open.controller;

import com.wlyuan.basic.api.ICustomerService;
import com.wlyuan.basic.dto.customer.CustomerFilterDTO;
import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.open.annotation.OpenApi;
import com.wlyuan.open.assembler.CustomerAssembler;
import com.wlyuan.open.basic.dto.OpenCustomerCreationDTO;
import com.wlyuan.open.basic.dto.OpenCustomerUpdateDTO;
import com.wlyuan.open.basic.vo.OpenCustomerDetailVO;
import com.wlyuan.open.translator.CustomerTranslator;
import com.wlyuan.utils.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author yuanjie
 */
@Api(tags = "发货方")
@Validated
@RequestMapping("/api/customer")
@RestController
public class OpenCustomerController {
    @DubboReference(check = false)
    private ICustomerService customerService;

    @ApiOperation("创建发货方")
    @PostMapping("/offline")
    @OpenApi
    public OpenCustomerDetailVO create(@RequestBody @Valid OpenCustomerCreationDTO openCustomer) {
        var offlineCustomer = CustomerAssembler.assemble(openCustomer);
        var customer = customerService.create(offlineCustomer);
        return CustomerTranslator.translate(customer);
    }

    @ApiOperation("修改发货方")
    @PutMapping("/offline")
    @OpenApi
    public void update(@RequestBody @Valid OpenCustomerUpdateDTO openCustomer) {
        var customer = CustomerAssembler.assemble(openCustomer);
        customerService.update(customer);
    }


    // todo 查询使用自己的DTO
    @ApiOperation(value = "查询发货方", notes = "精准查询")
    @PostMapping("/search")
    @OpenApi
    public Page<OpenCustomerDetailVO> search(@RequestBody @Valid CustomerFilterDTO customerFilter) {
        customerFilter.setCustomerNames(Arrays.asList(customerFilter.getCustomerName()));
        customerFilter.setCustomerName(null);
        customerFilter.setStatus(NamedStatus.ENABLED.getValue());
        var customers = customerService.search(customerFilter);
        List<OpenCustomerDetailVO> list = CustomerTranslator.translate(customers.getRecords());
        return Page.as(customers, list);
    }


}
